import Vue from 'vue'
import Vuex from 'vuex'

Vue.use(Vuex)

function createStore() {
  return new Vuex.Store({
    state: {
      loginlogList:'',//登录模块日志内容
      hintprofile:"default",
      audioprofile: "standard",
      profile: "480p", //视频清晰度
      showAnswerService: false, //是否展示客服通话弹窗页面
      isJoinRoom: false, //是否已经进过房了
      isLogin: false,
      loginUserInfo: null,
      // trtc 相关
      callStatus: 'idle', // 状态, idle, calling, connected
      callStatusText: '',
      isInviter: false, // c2c 通话，说不定一开始不是 inviter, 后面邀请了别人就是 inviter 了
      isAccepted: false,
      meetingUserIdList: [],
      muteVideoUserIdList: [],
      muteAudioUserIdList: [],
      text: '',
      userInfoDetails: null,
      currentUserId: '', //当前登录人的userid
      changeType: 0, //选择的通讯方式 0：未选择   1：语音  2：视频
      chatClient:'', //聊天的mq
      chatLog:[], //聊天日志
      chatArray:[],//聊天内容
      token:'',//百度接口token
    },
    mutations: {
      //设置聊天mq
      setChatClient(state, payload){
        state.chatClient = payload
      },
      //设置聊天日志
      setChatLog(state, payload){
        state.chatLog.push(payload)
      },
      //设置聊天列表
      setChatArray(state, payload){
        state.chatArray.push(payload)
      },
      // 删除没有发送出去的消息
      deleteErrorChat(state, id){
        state.chatArray = state.chatArray.filter(function(item) {
          return item.id !== id
        });
        console.log('state.chatArray',state.chatArray)
      },
      setChatArrayNull(state){
        state.chatArray=[]
      },
      //设置日志内容
      setLoginLogList(state, payload) {
        state.loginlogList = payload
      },
      //设置视频编码质量
      setHintProfile(state, payload) {
        state.hintprofile = payload
      },
      //设置视频清晰度
      setaudioprofile(state, payload) {
        state.audioprofile = payload
      },
      //设置视频清晰度
      setprofile(state, payload) {
        state.profile = payload
      },
      //设置是否打开客服通话页面
      setshowAnswerService(state, payload) {
        state.showAnswerService = payload
      },
      //设置通讯方式选择项
      setisJoinRoom(state, payload) {
        state.isJoinRoom = payload
      },
      //设置通讯方式选择项
      setChangeType(state, payload) {
        state.changeType = payload
      },
      //设置用户详细信息
      setcurrentUserId(state, payload) {
        state.currentUserId = payload
      },
      //设置用户详细信息
      setUserInfoDetails(state, payload) {
        state.userInfoDetails = payload
      },
      userLoginSuccess(state) {
        state.isLogin = true;
      },
      userLogoutSuccess(state) {
        state.isLogin = false;
        state.loginUserInfo = null;
        state.userInfoDetails = null;
      },
      setLoginUserInfo(state, payload) {
        const {
          userId,
          userSig
        } = payload;
        state.loginUserInfo = {
          userId,
          userSig
        }
      },
      updateIsInviter(state, isInviter) {
        state.isInviter = isInviter;
      },
      updateCallStatus(state, callStatus) {
        state.callStatus = callStatus;
        if (callStatus == 'calling') {
          state.callStatusText = '呼叫中'
        } else if (callStatus == 'connected') {
          state.callStatusText = '通话中'
        } else {
          state.callStatusText = ''
        }
      },
      userJoinMeeting(state, userId) {
        if (state.meetingUserIdList.indexOf(userId) === -1) {
          state.meetingUserIdList.push(userId);
        }
      },
      userAccepted(state, isAccepted) {
        state.isAccepted = isAccepted;
      },
      userLeaveMeeting(state, userId) {
        const index = state.meetingUserIdList.findIndex(item => item === userId);
        if (index >= 0) {
          state.meetingUserIdList.splice(index, 1);
        }
      },
      dissolveMeeting(state) {
        state.meetingUserIdList = [];
        state.isMuteVideoUserIdList = [];
        state.isMuteAudioUserIdList = [];
      },
      updateMuteVideoUserIdList(state, userIdList) {
        state.muteVideoUserIdList = userIdList;
      },
      updateMuteAudioUserIdList(state, userIdList) {
        state.muteAudioUserIdList = userIdList;
        state.text = "我被修改了" + userIdList
      },
      // 设置百度语音合成token
      setToken(state, token) {
        state.token = token
      }
    }
  });
}

const store = createStore();
export default store;